#include<bits/stdc++.h>
using namespace std;

int T, X[15];
int N, M, K;
int gen[100005];
int a[100005], c[100005], k[100005];
char s[33][100005];
bool p1 = 1;
set<int> st;

int binary(int x){
	int l = 0, r = 28, res = 0;
	while (l <= r){
		int mid = (l + r) >> 1;
		if ((1 << mid) >= x){
			res = mid;
			r = mid - 1;
		}else{
			l = mid + 1;
		}
	}
	return res;
}

long long solve(int l, int r, int K){
	long long res = 0;
	queue<int> q1, q2;
	for (int i = l; i <= r; i ++){
		q1.push(i);
	}
	for (int i = 1; i <= K; i ++){
		for (int j = 1; j <= (1 << (K - i)); j ++){
			int k1 = q1.front(); q1.pop();
			int k2 = q1.front(); q1.pop();
			// cout << i << " " << j << " " << k1 << " " << k2 << ";;\n";
			if (s[i][j] == '0'){
				if (a[k1] >= i) q2.push(k1);
				else q2.push(k2);
			}else{
				if (a[k2] >= i) q2.push(k2);
				else q2.push(k1);
			}
		}
		swap(q1, q2);
	}
	res += q1.front();
	return res;
}

void dfs(int now, int maxn, int op){
	if (now == maxn + 1){
		st.insert(solve(1, maxn, op));
		return;
	}
	for (int i = 0; i <= 5; i ++){
		int tmp = a[now];
		a[now] = i;
		dfs(now + 1, maxn, op);
		a[now] = tmp;
	}
}

int main(){
	freopen("arena.in", "r", stdin);
	freopen("arena.out", "w", stdout);
	
	scanf("%d %d", &N, &M);
	for (int i = 1; i <= N; i ++){
		scanf("%d", &gen[i]);
	}
	for (int i = 1; i <= M; i ++){
		scanf("%d", &c[i]);
		k[i] = binary(c[i]);
		p1 &= ((1 << (binary(c[i]))) == c[i]);
		// cout << c[i] << " " << k[i] << "[]\n";
	}
	K = binary(N);
	for (int i = 1; i <= K; i ++){
		scanf("%s", s[i] + 1);
	}
	scanf("%d", &T);
	while (T --){
		for (int i = 0; i < 4; i ++) scanf("%d", &X[i]);
		for (int i = 1; i <= N; i ++){
			a[i] = gen[i] ^ X[i % 4];
			a[i] = min(a[i], 33);
		}
		long long ans = 0;
		if(p1){
			long long rem[35] = {0};
			for (int i = 1; i <= M; i ++){
				if (rem[k[i]] == 0) rem[k[i]] = solve(1, c[i], k[i]);
				ans ^= 1LL * i * rem[k[i]];
			}
		}else{
			for (int i = 1; i <= M; i ++){
				st.clear();
				int len = (1 << k[i]);
				dfs(c[i] + 1, len, k[i]);
				int sum = 0;
				for (auto && i : st){
					sum += i;
				}
				ans ^= 1LL * i * sum;
			}
		}
		printf("%lld\n", ans);
	}

	return 0;
}
